package com.songyuquan.webcs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.songyuquan.webcs.pojo.User;
import com.songyuquan.webcs.pojo.UserAddress;
import com.songyuquan.webcs.service.CartService;
import com.songyuquan.webcs.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 用户地址控制器
 */
@Controller
@RequestMapping("/userAddress")
public class UserAddressController {

    @Autowired
    private CartService cartService;

    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/deleteById/{id}")
    public String deleteAddress(@PathVariable("id") Integer id) {
        userAddressService.removeById(id);
        return "redirect:/userAddressList";
    }

    @GetMapping("/addPage")
    public ModelAndView getAddPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        modelAndView.setViewName("userAddressAdd");
        return modelAndView;
    }

    @PostMapping("/addAddress")
    public String setAddAddress(
            @RequestParam("address") String address,
            @RequestParam("remark") String remark,
            @RequestParam("isdefault") Integer isdefault,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        UserAddress userAddress = new UserAddress();
        userAddress.setAddress(address);
        userAddress.setRemark(remark);
        userAddress.setUserId(user.getId());
        if (isdefault == 1) {
            QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("isdefault", 1);
            UserAddress one = userAddressService.getOne(queryWrapper);
            one.setIsdefault(0);
            userAddressService.updateById(one);
        }
        userAddress.setIsdefault(isdefault);
        userAddressService.save(userAddress);
        return "redirect:/userAddressList";
    }

    @GetMapping("/updatePage/{id}")
    public ModelAndView getUpdatePage(@PathVariable("id") Integer id, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        UserAddress userAddress = userAddressService.getById(id);
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        modelAndView.addObject("userAddress", userAddress);
        modelAndView.setViewName("userAddressUpdate");
        return modelAndView;
    }

    @PostMapping("/updateAddress")
    public String setUpdateAddress(
            @RequestParam("id") Integer id,
            @RequestParam("address") String address,
            @RequestParam("remark") String remark,
            @RequestParam("isdefault") Integer isdefault,
            HttpSession session
    ) {
        UserAddress userAddress = userAddressService.getById(id);
        if (userAddress.getIsdefault() != 1 && isdefault == 1) {
            QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("isdefault", 1);
            UserAddress one = userAddressService.getOne(queryWrapper);
            one.setIsdefault(0);
            userAddressService.updateById(one);
        }
        userAddress.setAddress(address);
        userAddress.setRemark(remark);
        userAddress.setIsdefault(isdefault);
        userAddressService.updateById(userAddress);
        return "redirect:/userAddressList";
    }
}

